package org.geektimes.projects.user.service.impl;

import java.sql.SQLException;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.DatabaseUserRepository;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.sql.DBConnectionManager;
import org.geektimes.projects.user.utils.DbUtils;

/**
 * @Desc: 用户业务类
 * @author: liuawei
 * @date: 2021-03-01 16:52
 */
public class UserServiceImpl implements UserService {

    @Override
    public boolean register(User user) throws SQLException, ClassNotFoundException {
        DBConnectionManager connectionManager = new DBConnectionManager();
        connectionManager.setConnection(DbUtils.getNativeConnection());
//        connectionManager.setConnection(DbUtils.getJNDIConnection());
        DatabaseUserRepository userRepository = new DatabaseUserRepository(connectionManager);
        // 先查找在入库
        User userDb = userRepository.getByNameAndPassword(user.getName(), user.getPassword());
        if (userDb == null) {
            userRepository.save(user);
            return true;
        }
        return false;
    }

    @Override
    public boolean deregister(User user) {
        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }

    @Override
    public User queryUserById(Long id) {
        return null;
    }

    @Override
    public User queryUserByNameAndPassword(String name, String password) {
        return null;
    }
}
